//
//  WelcomeView.swift
//  loveHHEveryDay
//
//  Created by yw on 2025/4/7.
//

import SwiftUI

struct WelcomeView: View {
    @State var currentValue:CGFloat = 1
    @State var totalValue:CGFloat = 3
    @State var birthDay:Date = .now
    
    @State var list:[[String:String]] = [
        [
            "icon":"wel_boy",
            "title":"男",
            "value":"man"
        ],
        [
            "icon":"wel_girl",
            "title":"女",
            "value":"women"
        ],
    ]
    @State var currentSex:Int = 0
    @State var userName:String = ""
    @State var showTabbarView:Bool = false
    
    @AppStorage(kUserDateName) var name:String = ""
    @AppStorage(kUserDateAge) var age:String = ""
    @AppStorage(kUserDateSex) var sex:String = ""
    @AppStorage(kUserDateLogin) var login:Bool = false
    @AppStorage(kUserDateId) var userId:String = ""
    @AppStorage(kUserDateIcon) var icon:String = ""
    @AppStorage(kUserToken) var token:String = ""
    @AppStorage(kUserIconBase64) var iconBase64:String = ""
    
    @StateObject var loginVM:LoginViewModel = LoginViewModel()
    
    var body: some View {
        VStack(alignment:.leading) {
            if currentValue != 1 {
                Image(systemName: "arrow.backward.square.fill")
                    .resizable()
                    .frame(width: 24, height: 24)
                    .accentColor(Color("color_text_main"))
                    .shakeTapGes {
                        withAnimation {
                            currentValue -= 1
                        }
                    }
                    .padding(.top,20)
                
            } else {
                RoundedRectangle(cornerRadius: 0)
                    .fill(Color("color_bg"))
                    .frame(width: 24,height: 24)
                    .padding(.top,20)
            }
                
            LinearProgressView(value: currentValue / totalValue, shape: .capsule)
                .tint(Gradient(colors: [Color("color_pink_01"),Color("color_pink_02")]))
                .frame(width: 300, height: 10)
                .padding(.top,46)
            
           
//            ChooseSexView()
           
            if currentValue == 1 {
                Text("Hello, 欢迎来到App")
                    .modifier(YWTextModifer(fontSize: 26, fontWeight: .bold, design: .rounded,color: Color("color_text_main")))
                    .padding(.top,36)
                Text("请选择您正确性别")
                    .modifier(YWTextModifer(fontSize: 16, fontWeight: .light, design: .rounded,color: Color("color_text_sub")))
                    .padding(.top,8)
                ChooseSexView(currentIndex: $currentSex, list: list)
//                    .transition(AnyTransition.asymmetric(insertion: AnyTransition.move(edge: .leading), removal: AnyTransition.move(edge: .leading)))
                    
            } else if currentValue == 2 {
                Text("您的生日在哪一天呢")
                    .modifier(YWTextModifer(fontSize: 26, fontWeight: .bold, design: .rounded,color: Color("color_text_main")))
                    .padding(.top,36)
                Text("请选择您正确性别确性别确性别确性别确性别")
                    .modifier(YWTextModifer(fontSize: 16, fontWeight: .light, design: .rounded,color: Color("color_text_sub")))
                    .padding(.top,8)

                ChooseAgeView(birthday: $birthDay)
//                    .transition(AnyTransition.asymmetric(insertion: AnyTransition.move(edge: .trailing), removal: AnyTransition.move(edge: .leading)))
            } else if currentValue == 3 {
                Text("填写您的用户昵称吧")
                    .modifier(YWTextModifer(fontSize: 26, fontWeight: .bold, design: .rounded,color: Color("color_text_main")))
                    .padding(.top,36)
                Text("请选择您正确性别请选择您正确性别")
                    .modifier(YWTextModifer(fontSize: 16, fontWeight: .light, design: .rounded,color: Color("color_text_sub")))
                    .padding(.top,8)
                ChooseNameView(userName: $userName)
//                    .transition(AnyTransition.move(edge: .trailing))
            }
            
            VStack(alignment: .center){
                ShakeButton(label:Text(currentValue >= 3 ? "完成":"下一步"), textColor: Color("color_text_main"),fontSize: 18,fontWeight: .bold) {
                    nextBtnClick()
                }
            }.frame(maxWidth: .infinity)
            Spacer()
        }
        .frame(maxWidth: .infinity)
        .padding(.horizontal,40)
        .background(Color("color_bg"))
        .fullScreenCover(isPresented: $showTabbarView, content: {
            TabBarView()
        })
        
//            .background(.red)
        
//        ProgressView(value: 5.0)
//            .accentColor(.red)
//            .frame(width: 300, height: 40)
//            .scaleEffect(x: 1, y: 2, anchor: .center)
       
    }
    
    private func nextBtnClick() {
        if currentValue >= 3 {
            if userName.isEmpty {
                YWToast.show(text: "您的名称不能为空哦~")
                return
            }
            let id = generateUniqueID(length: 10)
            let dict = [
                "name":userName,
                "userId":id,
                "birthday" : String().date2String(birthDay,dateFormat: .format_yMd),
                "sex" : currentSex == 1 ? "1":"0",
                "image":UIImage(named: currentSex == 1 ? "wel_boy":"wel_girl")?.base64 ?? ""
            ]
            loginVM.registUser(user: dict) { token in
                userId = id
                name = userName
                age = String().date2String(birthDay,dateFormat: .format_yMd)
                sex = currentSex == 1 ? "1":"0"
                icon = currentSex == 0 ? "wel_boy":"wel_girl"
                iconBase64 = UIImage(named: icon)?.base64 ?? ""
                self.token = token
                login = true
                showTabbarView.toggle()
            }
            return
        }
        withAnimation {
            currentValue += 1
        }
        
        
    }
    
    
}



//#Preview {
//    WelcomeView()
//}
